//
//  YanNavigationController.swift
//  SinaWeibo
//
//  Created by mac on 2017/4/13.
//  Copyright © 2017年 mac. All rights reserved.
//

import UIKit

class YanNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 隐藏默认的 navigationBar
        navigationBar.isHidden = true
    }
    
    // 重写 push 方法 ，所有push动作都会调用此方法
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        // 如果不是栈底控制器才需要隐藏，跟控制器不需要隐藏
        // 隐藏底部的tabbar
        if childViewControllers.count > 0 {
            
            viewController.hidesBottomBarWhenPushed = true
            
            
            // 判断控制器的类型
            if let vc = viewController as? YanBaseViewController {
                
                var title = "返回"
                // 判断控制器的级数
                if childViewControllers.count == 1{
                    title = childViewControllers.first?.title ?? "返回"
                }
                // 取出自定义的naviItem
                vc.naviItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popToParent), isBack: true)
            }
            
        }
        
        
        
        super.pushViewController(viewController, animated: true)
    }
    
    @objc fileprivate func popToParent() {
        popViewController(animated: true)
    }

}
